package com.yugao.fintech.antelope.demo.mq_concurrence;

import com.rabbitmq.client.Channel;
import com.yugao.fintech.framework.rabbitmq.RabbitMqCons;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@RabbitListener(bindings = {
        @QueueBinding(
                value = @Queue(value = "test.mq_concurrence", durable = "true"), key = "test",
                exchange = @Exchange(value = "test", type = ExchangeTypes.TOPIC))
}, containerFactory = RabbitMqCons.BEAN_CUSTOM_CONTAINER_FACTORY)
public class MqConcurrenceListener {

    @RabbitHandler
    public void onMessage(Long data, Channel channel, Message message) throws Exception {
        TimeUnit.MILLISECONDS.sleep(100);
        long sendTime = Long.parseLong(String.valueOf(data));
        log.info("消费成功: {}, time: {} ms", data, System.currentTimeMillis() - sendTime);
    }
}
